import { formatters, validators } from '../composables/useUtils'

/**
 * 优惠券工具函数
 */
export const couponUtils = {
  /**
   * 格式化优惠券金额
   */
  formatAmount: (amount: number): string => {
    return formatters.money(amount)
  },
  
  /**
   * 格式化折扣
   */
  formatDiscount: (discount: number): string => {
    return formatters.percent(discount)
  },
  
  /**
   * 获取优惠券类型文本
   */
  getTypeText: (type: number): string => {
    const typeMap = {
      1: '减免券',
      2: '折扣券',
      3: '礼品券'
    }
    return typeMap[type as keyof typeof typeMap] || '未知类型'
  },
  
  /**
   * 获取优惠券类型颜色
   */
  getTypeColor: (type: number): string => {
    const colorMap = {
      1: 'var(--success)',
      2: 'var(--info)',
      3: 'var(--warning)'
    }
    return colorMap[type as keyof typeof colorMap] || 'var(--text-muted)'
  },
  
  /**
   * 获取优惠券状态文本
   */
  getStatusText: (status: number): string => {
    const statusMap = {
      0: '未使用',
      1: '已使用',
      2: '已过期'
    }
    return statusMap[status as keyof typeof statusMap] || '未知状态'
  },
  
  /**
   * 获取优惠券状态颜色
   */
  getStatusColor: (status: number): string => {
    const colorMap = {
      0: 'var(--success)',
      1: 'var(--info)',
      2: 'var(--text-muted)'
    }
    return colorMap[status as keyof typeof colorMap] || 'var(--text-muted)'
  },
  
  /**
   * 获取库存状态
   */
  getInventoryStatus: (inventory: number): { text: string; color: string; class: string } => {
    if (inventory <= 0) {
      return { text: '已售罄', color: 'var(--danger)', class: 'empty' }
    } else if (inventory <= 10) {
      return { text: '库存紧张', color: 'var(--warning)', class: 'low' }
    } else {
      return { text: '库存充足', color: 'var(--success)', class: 'normal' }
    }
  },
  
  /**
   * 计算优惠券价值
   */
  calculateValue: (coupon: {
    couponType: number
    discountAmount?: number
    discount?: number
  }): string => {
    if (coupon.couponType === 1 && coupon.discountAmount) {
      return couponUtils.formatAmount(coupon.discountAmount)
    } else if (coupon.couponType === 2 && coupon.discount) {
      return couponUtils.formatDiscount(coupon.discount)
    }
    return '-'
  }
}

/**
 * 表格工具函数
 */
export const tableUtils = {
  /**
   * 生成表格序号
   */
  generateIndex: (currentPage: number, pageSize: number, index: number): number => {
    return (currentPage - 1) * pageSize + index + 1
  },
  
  /**
   * 格式化表格日期
   */
  formatDate: (dateStr: string, format = 'YYYY-MM-DD HH:mm:ss'): string => {
    return formatters.date(dateStr, format)
  },
  
  /**
   * 格式化表格金额
   */
  formatMoney: (amount: number): string => {
    return formatters.money(amount)
  }
}

/**
 * 表单工具函数
 */
export const formUtils = {
  /**
   * 验证手机号
   */
  validatePhone: (phone: string): boolean => {
    return validators.phone(phone)
  },
  
  /**
   * 验证邮箱
   */
  validateEmail: (email: string): boolean => {
    return validators.email(email)
  },
  
  /**
   * 验证身份证
   */
  validateIdCard: (idCard: string): boolean => {
    return validators.idCard(idCard)
  },
  
  /**
   * 验证正整数
   */
  validatePositiveInteger: (value: string): boolean => {
    return validators.positiveInteger(value)
  },
  
  /**
   * 获取表单验证规则
   */
  getFormRules: () => ({
    phone: [
      { required: true, message: '请输入手机号', trigger: 'blur' },
      { validator: (_rule: any, value: string) => validators.phone(value), message: '手机号格式不正确', trigger: 'blur' }
    ],
    email: [
      { required: true, message: '请输入邮箱', trigger: 'blur' },
      { validator: (_rule: any, value: string) => validators.email(value), message: '邮箱格式不正确', trigger: 'blur' }
    ],
    idCard: [
      { required: true, message: '请输入身份证', trigger: 'blur' },
      { validator: (_rule: any, value: string) => validators.idCard(value), message: '身份证格式不正确', trigger: 'blur' }
    ],
    positiveInteger: [
      { required: true, message: '请输入正整数', trigger: 'blur' },
      { validator: (_rule: any, value: string) => validators.positiveInteger(value), message: '请输入正整数', trigger: 'blur' }
    ]
  })
}